/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelCliente2.java
 *
 * Created on 24-dic-2011, 9:59:19
 */
package incidencias;

import accesoBD.QueryUsuario;
import idiomas.messages;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;

/**
 *
 * @author Cristian
 */
public class PanelCliente2 extends javax.swing.JFrame {
    Cliente cliente;
    /** Creates new form PanelCliente2
     * @param query 
     */
    public PanelCliente2(QueryUsuario query) {
        cliente = new Cliente (query);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        ComboBoxModel = new DefaultComboBoxModel();
        try {
            ResultSet rset = cliente.GetAparatos();
            ComboBoxModel = new DefaultComboBoxModel();
            while (rset.next()) {
                ComboBoxModel.addElement(rset.getString(2) + "/" + rset.getString(3));
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelCliente2.class.getName()).log(Level.SEVERE, null, ex);
        }
        tipoAveria = new javax.swing.JComboBox();
        LimpiarButton = new javax.swing.JButton();
        EnviarButton = new javax.swing.JButton();
        HistorialButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        lenguageSelect = new javax.swing.JComboBox();
        lenguageLabel = new javax.swing.JLabel();
        ComboBoxModel1 = new DefaultComboBoxModel();
        try {
            ResultSet rset = cliente.GetSitios();
            ComboBoxModel1 = new DefaultComboBoxModel();
            while (rset.next()) {
                ComboBoxModel1.addElement(rset.getString(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelCliente2.class.getName()).log(Level.SEVERE, null, ex);
        }
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        Descripcion = new javax.swing.JTextArea();
        salirButton1 = new javax.swing.JButton();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList1);

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tipoAveria.setModel(ComboBoxModel);

        LimpiarButton.setText("Limpiar");
        LimpiarButton.setPreferredSize(new java.awt.Dimension(100, 40));
        LimpiarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LimpiarButtonActionPerformed(evt);
            }
        });

        EnviarButton.setText("Enviar");
        EnviarButton.setPreferredSize(new java.awt.Dimension(100, 40));
        EnviarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EnviarButtonActionPerformed(evt);
            }
        });

        HistorialButton.setText("Historico");
        HistorialButton.setPreferredSize(new java.awt.Dimension(100, 40));
        HistorialButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HistorialButtonActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/incidencias/masterlogo.png"))); // NOI18N

        lenguageSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Español", "English" }));
        lenguageSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lenguageSelectActionPerformed(evt);
            }
        });

        lenguageLabel.setText("Idioma:");

        jComboBox1.setModel(ComboBoxModel1);
        jComboBox1.setToolTipText("");
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        Descripcion.setColumns(20);
        Descripcion.setRows(5);
        Descripcion.setText("Por favor, detalle su descripcion.");
        jScrollPane1.setViewportView(Descripcion);

        salirButton1.setText("Salir");
        salirButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(lenguageLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lenguageSelect, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(tipoAveria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(111, 111, 111)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 211, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(HistorialButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(LimpiarButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(EnviarButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(salirButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lenguageLabel)
                            .addComponent(lenguageSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tipoAveria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(salirButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LimpiarButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addComponent(EnviarButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(HistorialButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
    * Controlador encargado del botón Limpiar que se encarga de limpiar el area
    * de texto para poder insertar una descripcion despues de solicitar el historico 
    * o simplemente para limpiar la pantalla.
    * @param evt 
    */
    private void LimpiarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LimpiarButtonActionPerformed
        // TODO add your handling code here:
        //Descripcion.setText("Por favor, detalle su descripcion.");
        Descripcion.setEditable(true);
        Descripcion.setAutoscrolls(false);
        EnviarButton.setEnabled(true);
        String Idioma = (String) lenguageSelect.getSelectedItem();
        switch(lenguageSelect.getSelectedIndex()){
            case 0: //Español
                Idioma="ES";
                break;
            case 1: //Ingles
                Idioma="EN";
                break;
        }
        Locale locale = new Locale(Idioma);
        messages.setLocale(locale);
        Descripcion.setText(messages.getString("InsertYourDescription"));
    }//GEN-LAST:event_LimpiarButtonActionPerformed
    /**
    * Controlador encargado del botón Enviar que se encarga de crear nuevos partes 
    * en la base de datos
    * @param evt 
    */
    private void EnviarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EnviarButtonActionPerformed
        try {
            if (tipoAveria.getItemCount()>0){
                    String[] x = tipoAveria.getSelectedItem().toString().split("/");
                    cliente.CrearParte(cliente.GetAparatoIdprivado(x[1]), Descripcion.getText());
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelCliente2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_EnviarButtonActionPerformed
    /**
    * Controlador encargado de la lista de seleccion del idioma. 
    * Se encarga de la internacionalizacion del panel.
    * @param evt 
    */
    private void lenguageSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lenguageSelectActionPerformed
        // TODO add your handling code here:
        String Idioma = (String) lenguageSelect.getSelectedItem();
        switch(lenguageSelect.getSelectedIndex()){
            case 0: //Español
                Idioma="ES";
                break;
            case 1: //Ingles
                Idioma="EN";
                break;
        }
        Locale locale = new Locale(Idioma);
        messages.setLocale(locale);
        EnviarButton.setEnabled(true);
        lenguageLabel.setText(messages.getString("Language"));
        LimpiarButton.setText(messages.getString("Clear"));
        EnviarButton.setText(messages.getString("Send"));
        HistorialButton.setText(messages.getString("Record"));
        Descripcion.setText(messages.getString("InsertYourDescription"));
    }//GEN-LAST:event_lenguageSelectActionPerformed
    /**
    * Controlador encargado del botón Historial que se encarga de solicitar y 
    * representar un historico de los partes del usuario en el panel. 
    * o simplemente para limpiar la pantalla.
    * @param evt 
    */
    private void HistorialButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HistorialButtonActionPerformed
        try {
            Descripcion.setEditable(false);
            Descripcion.setAutoscrolls(true);
            EnviarButton.setEnabled(false);
            ResultSet r = cliente.GetPartes();
            Descripcion.setText("");
            while (r.next()) {
                Descripcion.setText(Descripcion.getText()+"\n"+"ID Parte: "+r.getString(1)+"\n"+"Aparato: "+cliente.GetAparatoIdpublico(r.getString(3))+"\n"+"Estado: "+r.getString(2)+"\n-----------------------------------------");
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelCliente2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_HistorialButtonActionPerformed
    /**
    * Controlador encargado derealizar un filtrado de los aparatos que se muestran
    * en la lista de apartos a partir de una sala.
    * @param evt 
    */
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        ComboBoxModel = new DefaultComboBoxModel();
        try {
            ResultSet rset = cliente.GetAparatos(jComboBox1.getSelectedItem().toString());
            ComboBoxModel = new DefaultComboBoxModel();
            while (rset.next()) {
                ComboBoxModel.addElement(rset.getString(2) + "/" + rset.getString(3));
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelCliente2.class.getName()).log(Level.SEVERE, null, ex);
        }
        //tipoAveria = new javax.swing.JComboBox();

        tipoAveria.setModel(ComboBoxModel);
    }//GEN-LAST:event_jComboBox1ActionPerformed
    
    /**
     * Controlador encargado del botón salir, que nos devuelve a la pantalla de login
     * @param evt 
     */
    private void salirButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirButton1ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        login.main(null);
    }//GEN-LAST:event_salirButton1ActionPerformed

    /**
     * @param args the command line arguments
     * @param query QueryUsuario es el objeto para realizar las consultas con la base de datos.
     */
    public static void main(String args[],final QueryUsuario query) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PanelCliente2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PanelCliente2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PanelCliente2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PanelCliente2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                PanelCliente2 pc2 = new PanelCliente2(query);
                pc2.setResizable(false);
                pc2.setVisible(true);                                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea Descripcion;
    private javax.swing.JButton EnviarButton;
    private javax.swing.JButton HistorialButton;
    private javax.swing.JButton LimpiarButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lenguageLabel;
    private javax.swing.JComboBox lenguageSelect;
    private javax.swing.JButton salirButton1;
    private javax.swing.JComboBox tipoAveria;
    // End of variables declaration//GEN-END:variables
    private DefaultComboBoxModel ComboBoxModel;
    private DefaultComboBoxModel ComboBoxModel1;
}
